import Emberbox from "@/utils/mapbox/engine";
import { baseurl } from "../../config";

export default function buildingsManage(emberbox: Emberbox) {
  let data: any = null,
    buildings: any = null,
    isShow: boolean = true;
  fetch(`${baseurl}/buildings`)
    .then((res) => res.json())
    .then((res: any) => {
      data = res;
      buildings = emberbox.geojsonManage?.addIndoorJson(res);
      isShow = true;
      console.log("buildings", buildings);
    })
    .catch((err) => {
      console.log("数据获取失败：buildings", err);
    });

  emberbox.on("onZoom", (e: number) => {
    if (data === null) return;
    if (e > 18.3 && isShow) {
      isShow = false;
      emberbox.geojsonManage?.removeJson(buildings);
      buildings = null;
    }

    if (e < 18 && isShow === false) {
      isShow = true;
      buildings = emberbox.geojsonManage?.addIndoorJson(data);
    }
  });
}
